<?php
class m_top extends spModel{
	function sessionUpdate($nick,$sessionkey,$appkey){
		$this->tbl_name = PREFIX.'topsession';
		$this->pk = 'id';
		if($this->find(array('nick'=>$nick)) !== FALSE){
			return $this -> update(array('nick'=>$nick), array('sessionkey'=>$sessionkey,'live'=>'1','appkey'=>$appkey));
		}
		else{
			return $this -> create(array('nick'=>$nick,'sessionkey'=>$sessionkey,'live'=>'1','appkey'=>$appkey));
		}
	}
	
	function sessionInvilid($nick){
		$this->tbl_name = PREFIX.'topsession';
		$this->pk = 'id';
		return $this -> update(array('nick'=>$nick), array('live'=>'0'));
	}
	
	function getSessions($id = NULL,$nick = NULL,$live = '1'){
		$this->tbl_name = PREFIX.'topsession';
		$this->pk = 'id';
		$conditions = array('id'=>$id,'nick'=>$nick,'live'=>$live);
		$conditions = $this -> trim($conditions);
		return $this -> findAll($conditions);
	}
	
	/**
	 * trim
	 * 将无值的ROW数组元素删去
	 * 值为NULL即不更新
	 * @param array $array
	 */
	function trim($array){
		foreach ($array as $k => $v){
			if($v === NULL) unset($array[$k]);
		}
		return $array;
	}
}